package com.hw.one.collect.anyang.job;

import com.hw.one.collect.anyang.data.AnYangPushService;
import com.hw.one.collect.config.ConditionConfig;
import com.hw.one.collect.util.HttpClientUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/05/17 14:37.
 *
 * 安阳扬尘推送服务
 */
@Component
public class AnYangPushJob {

    private Logger log = Logger.getLogger(this.getClass());
    @Resource
    private AnYangPushService anYangPushService;
    /**
     * 安阳扬尘推送 定时任务
     */
    public void start() {
        // 读取配置文件
        String snCodes = ConditionConfig.get("SNCODE", "anyang");
        String pushUrl = ConditionConfig.get("PUSH_URL", "anyang");
        String[] snCodes2 = snCodes.split(",");
        List<String> snCodeList = new ArrayList<>();
        Collections.addAll(snCodeList, snCodes2);
        List<String> resultList = anYangPushService.findFiveMinData(snCodeList);

        for (String result : resultList) {
            String pushUrlWithParam = pushUrl + "?" + result;
            log.info("send url: " + pushUrlWithParam);
            // 发送数据
            for (int i=0; i<5; i++) {
                Map<String, String> resultMap = HttpClientUtil.sendGet(pushUrlWithParam, new HashMap<String, String>());
                log.info(resultMap.toString());
                if ("success".equals(resultMap.get("result"))) {
                    break;
                }
            }
        }


    }
}
